package com.czp.ssm.service.impl;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.czp.ssm.bean.Aircraft;
import com.czp.ssm.bean.Company;
import com.czp.ssm.bean.Opera;
import com.czp.ssm.bean.Plane;
import com.czp.ssm.bean.PlaneExample;
import com.czp.ssm.bean.PlaneExample.Criteria;
import com.czp.ssm.bean.PlaneOrder;
import com.czp.ssm.bean.PlaneOrderExample;
import com.czp.ssm.bean.User;
import com.czp.ssm.dao.CompanyMapper;
import com.czp.ssm.dao.OperaMapper;
import com.czp.ssm.dao.PlaneMapper;
import com.czp.ssm.dao.PlaneOrderMapper;
import com.czp.ssm.dao.UserMapper;
import com.czp.ssm.service.OperaService;
import com.czp.ssm.service.PlaneService;
import com.czp.ssm.utils.CommonUtils;
@Service
public class PlaneServiceImpl implements PlaneService {

	@Autowired
	private PlaneMapper planeMapper;
	
	@Autowired
	private PlaneOrderMapper planeOrderMapper;
	
	@Autowired
	private OperaService operaService;
	
	@Autowired
	private UserMapper userMapper;
	
	@Autowired
	private CompanyMapper companyMapper;
	
	@Autowired
	private OperaMapper operaMapper;
	
	public List<Plane> getPlaneListByCondition(Plane plane) {  
		PlaneExample planeExample = new PlaneExample();
		planeExample.setOrderByClause("PLANE_ID asc");
		planeExample.setDistinct(false);
		Criteria criteria = planeExample.createCriteria();
		criteria.andStartCityNameEqualTo(plane.getStartCityName());
		criteria.andEndCityNameEqualTo(plane.getEndCityName());
		criteria.andStartDateEqualTo(plane.getStartDate());
		List<Plane> planeList = planeMapper.selectByExample(planeExample);
		for (int i = 0; i < planeList.size(); i++) {
			String aircraft = planeList.get(i).getAircraftPosition();
			JSONArray jsonArray = JSONArray.parseArray(aircraft) ;
			List<Aircraft> aircrafts = jsonArray.toJavaList(Aircraft.class);
			planeList.get(i).setAircrafts(aircrafts);
		}
		return planeList;
	}


	public Plane getPlaneByCondition(String planeId,String aircraftId) {
		Plane plane = planeMapper.selectByPlaneId(planeId);
		String postion = plane.getAircraftPosition();
		JSONArray jsonArray = JSONArray.parseArray(postion) ;
		List<Aircraft> aircrafts = jsonArray.toJavaList(Aircraft.class);
		for (int i = 0; i < aircrafts.size(); i++) {
			Aircraft aircraft = aircrafts.get(i);
			if (aircraftId.equals(aircraft.getAircraftId())) {
				plane.setAircraft(aircraft);
				break;
			}
		}
		return plane;
	}


	public int savePlaneOrder(PlaneOrder planeOrder) {
		planeOrder.setOrderId(CommonUtils.getOrderId());
		planeOrder.setOrderStatus("0");//待审批
		planeOrder.setPayType("0");///为付款
		planeOrder.setIsquit("0");//是否改签过
		planeOrder.setCreateOrderDate(new Date());
		//获取对应的审批人ID
		Opera opera = operaService.getOperaPersonIdByCompanyIdAndUserId(planeOrder.getCompanyId(),planeOrder.getUserId());
		planeOrder.setOperaPersonId(opera.getOperaPersonId());
		int flag = planeOrderMapper.insert(planeOrder);
		return flag;
	}


	public List<PlaneOrder> findAllPlaneOrders() {
		PlaneOrderExample example = new PlaneOrderExample();
		example.setOrderByClause("ID asc");
		example.setDistinct(false);
		List<PlaneOrder> planeList = planeOrderMapper.selectByExample(example);
		for (int i = 0; i < planeList.size(); i++) {
			PlaneOrder planeOrder = planeList.get(i);
			String userId = planeOrder.getUserId();
			System.out.println("userId---------"+userId);
			User user = userMapper.selectUserByUserId(userId);
			planeOrder.setUserName(user.getUserName());
			planeOrder.setLinkPhone(user.getUserPhone());
			Map<String, String> map = new HashMap<String, String>();
			map.put("COMPANY_ID", planeOrder.getCompanyId());
			map.put("USER_ID", planeOrder.getUserId());
			Opera opera = operaMapper.selectOperaPersonIdByCompanyIdAndUserId(map);
			User operaUser = userMapper.selectUserByUserId(opera.getOperaPersonId());
			planeOrder.setOperaPersonName(operaUser.getUserName());
		}
		return planeList;
	}


	public int updatePlaneOrderStatus(String id,String status) {
		PlaneOrder planeOrder = new PlaneOrder();
		planeOrder.setId(Long.parseLong(id));
		planeOrder.setOrderStatus(status);
		int flag = planeOrderMapper.updateByPrimaryKeySelective(planeOrder);
		return flag;
	}
}
